package pkg.action;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
import java.io.ByteArrayInputStream;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import pkg.dao.EntityDAO;
import pkg.entity.Auction;
import pkg.entity.Teacher;
import pkg.util.ImageUtil;

public class ImageActionBean extends BaseActionBean {

    private Auction auction;
    private Teacher teacher;

    public ImageActionBean() {
    }

    @DefaultHandler
    public Resolution image() {
        EntityDAO dao = new EntityDAO();
        byte[] blob = null;
        if (auction != null) {
            auction = dao.find(Auction.class, auction.getId());
            blob = auction.getPicBlob();
        }
        if (teacher != null) {
            teacher = dao.find(Teacher.class, teacher.getId());
            blob = teacher.getPicBlob();
        }

        Image image = ImagesServiceFactory.makeImage(blob);
        return new StreamingResolution(ImageUtil.getContentType(image), new ByteArrayInputStream(image.getImageData()));
    }

    //<editor-fold defaultstate="collapsed" desc=" Setters/Getters ">
    public Auction getAuction() {
        return auction;
    }

    public void setAuction(Auction auction) {
        this.auction = auction;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    //</editor-fold>
}
